<?php
/**
 * Created by PhpStorm.
 * Script Name: Mptj.php
 * Create: 2023/3/7 13:47
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\tenant\controller;


use app\common\service\Tenant as TenantService;
use plugin\wechat\app\model\MpFollow as FollowM;
use support\Request;

class Mptj extends MpBase
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new FollowM();
    }

    /**
     * @param Request $request
     * @return mixed|\support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function index(Request $request){
        $mp_info = $request->mpInfo;
        $where = [
            TenantService::companyIdWhere(),
            ['mp_id' ,'=', $mp_info['id']],
        ];

        if(request()->isPost()){
            $cache_key = wechat_cache_key('followydaystj' . $mp_info['id']);
            $return = cache($cache_key);
            if(! $return){
                $end = strtotime(date('Ymd 00:00:00')) + 86400;
                $begin = $end - 7 * 86400;
                $x_data = [];
                for($i = $begin; $i <= $end; $i+=86400){
                    $x_data[] = date('m-d', $i);
                }
                //好友数据
                $legends = [];
                $series = [];
                $titles = ['新关注人数', '取关人数', '累积关注人数'];
                foreach ($titles as $k => $title){
                    $legends[] = $title;
                    $temp = [
                        'name' => $title,
                        'data' => []
                    ];
                    $series[] = $temp;
                }

                $total = null;
                for($i = $begin; $i <= $end; $i+=86400){
                    $where_subscribe = $where + [['subscribe', '=', 1], ['subscribe_time', 'between', [$i, $i + 86399]]];
                    $where_unsubscribe = $where + [['subscribe', '=', 0], ['unsubscribe_time', 'between', [$i, $i + 86399]]];
                    $where_total = $where;
                    array_push($where_subscribe, ['subscribe', '=', 1]);
                    array_push($where_subscribe, ['subscribe_time', 'between', [$i, $i + 86399]]);
                    array_push($where_unsubscribe, ['subscribe', '=', 0]);
                    array_push($where_unsubscribe, ['unsubscribe_time', 'between', [$i, $i + 86399]]);

                    $num_subscribe = $this->model->where($where_subscribe)->count();
                    $num_unsubscribe = $this->model->where($where_unsubscribe)->count();
                    if($total === null){
                        array_push($where_total, ['subscribe', '=', 1]);
                        array_push($where_total, ['subscribe_time', '<=', $i + 86399]);
                        $total = intval($this->model->where($where_total)->count());
                    }else{
                        $total = $total + ($num_subscribe - $num_unsubscribe);
                    }
                    $series[0]['data'][] = $num_subscribe;
                    $series[1]['data'][] = $num_unsubscribe;
                    $series[2]['data'][] = $total;
                }

                $return = ['xData' => $x_data, 'legends' => $legends, 'series' => $series];
                cache($cache_key, $return, 600);
            }
            return $this->success('', '', $return);
        }

        $today_begin = strtotime(date('Y-m-d'));
        $today_end = time();
        $yesterday_begin = $today_begin - 86400;
        $yesterday_end = $today_begin - 1;
        $yesterday_subscribe = $this->model->where($where)
            ->where('subscribe', 1)
            ->where('subscribe_time', 'between', [$yesterday_begin, $yesterday_end])
            ->count();
        $yesterday_unsubscribe = $this->model->where($where)
            ->where('subscribe', 0)
            ->where('unsubscribe_time', 'between', [$yesterday_begin, $yesterday_end])
            ->count();
        $yesterday_total = $this->model->where($where)
            ->where('subscribe', 1)
            ->where('subscribe_time', '<=', $yesterday_end)
            ->count();
        $today_subscribe = $this->model->where($where)
            ->where('subscribe', 1)
            ->where('subscribe_time', 'between', [$today_begin, $today_end])
            ->count();
        $today_unsubscribe = $this->model->where($where)
            ->where('subscribe', 0)
            ->where('unsubscribe_time', 'between', [$today_begin, $today_end])
            ->count();
        $today_total = $yesterday_total + ($today_subscribe - $today_unsubscribe);
        $assign = [
            'yesterday' => [
                'subscribe' => $yesterday_subscribe,
                'unsubscribe' => $yesterday_unsubscribe,
                'total' => $yesterday_total
            ],
            'today' => [
                'subscribe' => $today_subscribe,
                'unsubscribe' => $today_unsubscribe,
                'total' => $today_total
            ]
        ];
        return $this->show($assign);
    }
}